{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8db55ed6-5cb6-4a3d-b170-43a9cf06033b",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 36.步进电机驱动模块实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：36.步进电机驱动模块实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：步进电机驱动后模块实验\n",
    "步进电机驱动模块实验，gpiostepper步进电机驱动库，可以方便控制步进电机的速度、方向等！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7cba9f96-1e39-4c9b-8f3a-75f2f40bc474",
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import sleep\n",
    "from gpiostepper import Stepper"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.定义步进电机管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Speed: 300 rpm\n"
     ]
    }
   ],
   "source": [
    "speed = 300\n",
    "print(\"Speed: {} rpm\".format(speed))\n",
    "number_of_steps = 32\n",
    "step_motor1 = Stepper(motor_pins=[17, 18, 19, 20], number_of_steps = number_of_steps)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ecf06dd-3b8e-4925-8b8a-6b4facc7a032",
   "metadata": {},
   "source": [
    "## 3.初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d51bb77a-312f-4bf3-8d02-b892ec11c96b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Step Delay: 6.25ms\n"
     ]
    }
   ],
   "source": [
    "step_motor1.set_speed(speed)  # 设置速度\n",
    "amount_of_gear_reduction = 64\n",
    "number_of_steps_per_revolution_geared_output = number_of_steps * amount_of_gear_reduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49b9235a-0ecd-4e59-bae2-dbb73493c916",
   "metadata": {},
   "source": [
    "## 4.定义功能函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cdf51c59-79a3-4e8b-b4d5-b8dbc1ea1fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 步进电机旋转\n",
    "def makerobo_rotary(clb_direction):\n",
    "    if(clb_direction == 'a'):     # 逆时针旋转\n",
    "        step_motor1.step(number_of_steps_per_revolution_geared_output)\n",
    "        sleep(1)\n",
    "\n",
    "    elif(clb_direction == 'c'):    # 顺时针旋转\n",
    "        step_motor1.step(-number_of_steps_per_revolution_geared_output)\n",
    "        sleep(1)\n",
    "\n",
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "    while True:\n",
    "        clb_direction = input('Makerobo select motor direction a=anticlockwise, c=clockwise: ')\n",
    "        if(clb_direction == 'c'):\n",
    "            print('Makerobo motor running clockwise\\n')       # 顺时针旋转\n",
    "            break\n",
    "        elif(clb_direction == 'a'):\n",
    "            print('Makerobo motor running anti-clockwise\\n')  # 逆时针旋转\n",
    "            break\n",
    "        else:\n",
    "            print('Makerobo input error, please try again!') # 输入错误，再次输入\n",
    "    while True:\n",
    "        makerobo_rotary(clb_direction)       # 让步进电机旋转"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 5.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Makerobo select motor direction a=anticlockwise, c=clockwise:  a\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Makerobo motor running anti-clockwise\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "    try:\n",
    "        makerobo_loop()  # 循环函数\n",
    "    except KeyboardInterrupt:   # 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "        pass  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11f05e8b-75cb-4ade-93bb-aee745df03a2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb623807-e82b-40d0-8d83-caae2bda2e24",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
